using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace MauiBuilder.ViewModels
{
    /// <summary>
    /// 主页面的视图模型
    /// 使用CommunityToolkit.Mvvm实现MVVM模式，处理主页面的业务逻辑
    /// </summary>
    public partial class MainPageViewModel : BaseViewModel
    {
        [ObservableProperty]
        private int _count = 0;
        
        [ObservableProperty]
        private string _counterButtonText = "Click me";

        public MainPageViewModel()
        {
            // 简化构造函数，专注于主页面功能
        }

        #region RelayCommand 命令

        /// <summary>
        /// 处理计数器点击
        /// </summary>
        [RelayCommand]
        private void OnCounterClicked()
        {
            Count++;

            if (Count == 1)
                CounterButtonText = $"Clicked {Count} time";
            else
                CounterButtonText = $"Clicked {Count} times";
        }
        
        /// <summary>
        /// 导航到通知页面
        /// </summary>
        [RelayCommand]
        private async Task OnNavigateToNotification()
        {
            try
            {
                await Shell.Current.GoToAsync("//NotificationPage");
            }
            catch (Exception ex)
            {
                await AppShell.DisplayAlertAsync("错误", $"导航失败: {ex.Message}");
            }
        }
        
        /// <summary>
        /// 导航到前台服务页面
        /// </summary>
        [RelayCommand]
        private async Task OnNavigateToForegroundService()
        {
            try
            {
                await Shell.Current.GoToAsync("//ForegroundServicePage");
            }
            catch (Exception ex)
            {
                await AppShell.DisplayAlertAsync("错误", $"导航失败: {ex.Message}");
            }
        }
        
        /// <summary>
        /// 导航到Socket页面
        /// </summary>
        [RelayCommand]
        private async Task OnNavigateToSocket()
        {
            try
            {
                await Shell.Current.GoToAsync("//SocketPage");
            }
            catch (Exception ex)
            {
                await AppShell.DisplayAlertAsync("错误", $"导航失败: {ex.Message}");
            }
        }
        
        /// <summary>
        /// 导航到HybridWeb页面
        /// </summary>
        [RelayCommand]
        private async Task OnNavigateToHybridWeb()
        {
            try
            {
                await Shell.Current.GoToAsync("//HybridWebPage");
            }
            catch (Exception ex)
            {
                await AppShell.DisplayAlertAsync("错误", $"导航失败: {ex.Message}");
            }
        }
        
        /// <summary>
        /// 导航到聊天页面
        /// </summary>
        [RelayCommand]
        private async Task OnNavigateToChat()
        {
            try
            {
                await Shell.Current.GoToAsync("//ChatPage");
            }
            catch (Exception ex)
            {
                await AppShell.DisplayAlertAsync("错误", $"导航失败: {ex.Message}");
            }
        }
        
        /// <summary>
        /// 导航到语音转文本页面
        /// </summary>
        [RelayCommand]
        private async Task OnNavigateToSpeechToText()
        {
            try
            {
                await Shell.Current.GoToAsync("//SpeechToTextPage");
            }
            catch (Exception ex)
            {
                await AppShell.DisplayAlertAsync("错误", $"导航失败: {ex.Message}");
            }
        }

        #endregion
    }
}